import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import Config from '@/config'
let isFirstRequest = true
const service = axios.create({

  withCredentials: true // send cookies when cross-domain requests

})

// payEdit interceptor
service.interceptors.request.use(
  config => {
    config.params = {
      ...config.params,
      t: new Date().getTime()
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
const downUrl = ['mouldDownload', 'exportExcel', 'exportWord', '/human-manage/message/messagCardExport', '/professional-services/technician/downloadAllFiles',
'/professional-services/technician/exportWord', '/human-manage/generalManageQuery/expertPersonGeneralInfoListByCondition', '/human-manage/userinfo/exportGrowthPlan',
'/human-manage/flXztx/downloadFile', '/human-manage/flPtr/downloadFile']
// response interceptor
service.interceptors.response.use(
  response => {
    const res = response.data

    if (res && isFirstRequest) {
      // 判断是否登录超时
      // const chaoshi = `<script type='text/javascript'>top.location.href='http://iscsso.ah.sgcc.com.cn/isc_sso/login?service=http://25.50.160.80/engineering-project-info/projectInfo/projectInfoList';</script>`
      const isTimeout = /.*top.location.href='([^']*)/.test(res)
      if (isTimeout) {
        isFirstRequest = false
        const loginUrl = RegExp.$1
        MessageBox.alert('登录超时，请跳转到登录页面重新登录！', '登录超时', {
          confirmButtonText: '确定',
          callback: () => {
            top.location.href = loginUrl
          }
        })
        return
      }
    } else {
      return
    }

    // 导出接口返回的数据格式和别的不一样，另外做处理
    if (downUrl.find(v => response.config.url.indexOf(v) !== -1)) {
      return response
    }

    if (!res.success) {
      Message({
        type: 'warning',
        dangerouslyUseHTMLString: true,
        message: res.errorMsg
      })
      return Promise.reject(res.errorMsg || `error`)
    }
    return res
  },
  error => {
    if (error && error.message === Config.ABORMESSAGE) {
      return Promise.reject({
        data: '',
        errorMsg: Config.ABORMESSAGE,
        success: false
      })
    }
    const err = error.response || {}
    if (error.code === 'ECONNABORTED') {
      Message({
        message: '接口请求超时',
        type: 'error',
        duration: 2000
      })
      return Promise.reject({ message: '接口请求超时' })
    }

    if (!(err && err.status)) {
      err.message = '接口请求出错'
      Message({
        message: err.message,
        type: 'error',
        duration: 2000
      })
      return Promise.reject({ message: '接口请求出错' })
    }

    switch (err.status) {
      case 404:
        err.message = '接口地址不正确'
        break
      case 400:
        err.message = '请求参数有误'
        break
      case 401:
        err.message = '没有权限'
        break
      case 500:
        err.message = '服务器异常'
        break
      default:
        err.message = '请求出错'
    }
    Message({
      message: err.message,
      type: 'error',
      duration: 2000
    })
    return Promise.reject(err)
  }
)

export default service
